package com.zenithmind.user.pojo.domain;

import com.zenithmind.common.base.BaseEntity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.Objects;

/**
 * 权限聚合根
 * 遵循单一职责原则：只负责权限信息管理
 * 遵循封装原则：通过枚举和方法控制权限类型
 */
@EqualsAndHashCode(callSuper = true)
@Getter
@NoArgsConstructor
@Schema(name = "权限实体", description = "权限聚合根")
public class Permission extends BaseEntity {

    /**
     * 权限类型枚举
     */
    public enum PermissionType {
        MENU(1, "菜单"),
        BUTTON(2, "按钮"),
        API(3, "API");

        private final int code;
        private final String description;

        PermissionType(int code, String description) {
            this.code = code;
            this.description = description;
        }

        public int getCode() { return code; }
        public String getDescription() { return description; }

        public static PermissionType fromCode(int code) {
            for (PermissionType type : values()) {
                if (type.code == code) {
                    return type;
                }
            }
            throw new IllegalArgumentException("未知的权限类型代码: " + code);
        }
    }

    @Schema(description = "权限名称")
    private String permissionName;

    @Schema(description = "权限编码")
    private String permissionCode;

    @Schema(description = "父权限ID")
    private String parentId;

    @Schema(description = "权限类型")
    private Integer type;

    @Schema(description = "资源路径")
    private String resourcePath;

    @Schema(description = "图标")
    private String icon;

    @Schema(description = "排序")
    private Integer sortOrder;

    @Schema(description = "状态")
    private Integer status;

    /**
     * 构造函数 - 创建新权限
     */
    public Permission(String permissionName, String permissionCode, PermissionType type) {
        this.permissionName = Objects.requireNonNull(permissionName, "权限名称不能为空");
        this.permissionCode = Objects.requireNonNull(permissionCode, "权限编码不能为空");
        this.type = Objects.requireNonNull(type, "权限类型不能为空").getCode();
        this.status = 1; // 默认启用
        this.sortOrder = 0;

        validatePermissionName(permissionName);
        validatePermissionCode(permissionCode);
    }

    /**
     * 设置父权限
     */
    public void setParent(String parentId) {
        this.parentId = parentId;
    }

    /**
     * 设置资源路径
     */
    public void setResource(String resourcePath) {
        this.resourcePath = resourcePath;
    }

    /**
     * 设置图标
     */
    public void setIcon(String icon) {
        this.icon = icon;
    }

    /**
     * 设置排序
     */
    public void setSortOrder(Integer sortOrder) {
        this.sortOrder = sortOrder != null ? sortOrder : 0;
    }

    /**
     * 更新权限信息
     */
    public void updateInfo(String permissionName, String resourcePath, String icon) {
        if (permissionName != null) {
            validatePermissionName(permissionName);
            this.permissionName = permissionName;
        }
        this.resourcePath = resourcePath;
        this.icon = icon;
    }

    /**
     * 启用权限
     */
    public void enable() {
        this.status = 1;
    }

    /**
     * 禁用权限
     */
    public void disable() {
        this.status = 0;
    }

    /**
     * 检查权限是否启用
     */
    public boolean isEnabled() {
        return this.status != null && this.status == 1;
    }

    /**
     * 检查是否为根权限
     */
    public boolean isRoot() {
        return this.parentId == null || this.parentId.trim().isEmpty();
    }

    /**
     * 获取权限类型
     */
    public PermissionType getPermissionType() {
        return PermissionType.fromCode(this.type);
    }

    /**
     * 检查是否为菜单权限
     */
    public boolean isMenu() {
        return this.type != null && this.type == PermissionType.MENU.getCode();
    }

    /**
     * 检查是否为按钮权限
     */
    public boolean isButton() {
        return this.type != null && this.type == PermissionType.BUTTON.getCode();
    }

    /**
     * 检查是否为API权限
     */
    public boolean isApi() {
        return this.type != null && this.type == PermissionType.API.getCode();
    }

    /**
     * 验证权限名称
     */
    private void validatePermissionName(String permissionName) {
        if (permissionName == null || permissionName.trim().isEmpty()) {
            throw new IllegalArgumentException("权限名称不能为空");
        }
        if (permissionName.length() > 100) {
            throw new IllegalArgumentException("权限名称长度不能超过100个字符");
        }
    }

    /**
     * 验证权限编码
     */
    private void validatePermissionCode(String permissionCode) {
        if (permissionCode == null || permissionCode.trim().isEmpty()) {
            throw new IllegalArgumentException("权限编码不能为空");
        }
        if (permissionCode.length() > 100) {
            throw new IllegalArgumentException("权限编码长度不能超过100个字符");
        }
        if (!permissionCode.matches("^[a-z][a-z0-9_]*:[a-z][a-z0-9_]*$")) {
            throw new IllegalArgumentException("权限编码格式不正确，应为 module:action 格式");
        }
    }

    // MyBatis-Plus需要的setter方法（仅用于数据库映射）
    public void setPermissionName(String permissionName) { this.permissionName = permissionName; }
    public void setPermissionCode(String permissionCode) { this.permissionCode = permissionCode; }
    public void setParentId(String parentId) { this.parentId = parentId; }
    public void setType(Integer type) { this.type = type; }
    public void setResourcePath(String resourcePath) { this.resourcePath = resourcePath; }
    public void setStatus(Integer status) { this.status = status; }
}
